package me.zijunlin.sms.save.dao;


import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import me.zijunlin.sms.save.model.Sms;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.os.Environment;
import android.util.Xml;

public class XmlHelp {

	private XmlSerializer xmlSerializer;
	private StringWriter xmlWriter;
	private final String ROOT_XML = "Smses";
	private final String ELEMENT_XML = "Sms";
	private final String SMS_ID_XML = "Id";
	private final String SMS_PHONE_NUM_XML = "PhoneNumber";
	private final String CONTACT_DISPLAY_NAME_XML="DisplayName";
	private final String CONTACT_ID_XML="ContactId";
	private final String SMS_TIME_XML = "Time";
	private final String SMS_BODY_XML = "Content";
	private final String SMS_SAVE_TIME="SaveTime";

	public XmlHelp() {
		xmlSerializer = Xml.newSerializer();
		xmlWriter = new StringWriter();

	}

	public String formatToXml(List<Sms> smsList) {
		try {
			xmlSerializer.setOutput(xmlWriter);
			xmlSerializer.startDocument("UTF-8", true);
			if (!smsList.isEmpty()) {
				xmlSerializer.startTag("", ROOT_XML);
				for (Sms item : smsList) {
					if (item.IsSave()) {
						xmlSerializer.startTag("", ELEMENT_XML);
						formatSms(item);
						xmlSerializer.endTag("", ELEMENT_XML);
					}
				}
				xmlSerializer.endTag("", ROOT_XML);

			}
			xmlSerializer.endDocument();
			return xmlWriter.toString();

		} catch (Exception e) {

			throw new RuntimeException(e);
		}

	}

	private void formatSms(Sms sms) throws IllegalArgumentException,
			IllegalStateException, IOException {
		xmlSerializer.startTag("", SMS_ID_XML);
		xmlSerializer.text(Integer.toString(sms.getSmsId()));
		xmlSerializer.endTag("", SMS_ID_XML);

		xmlSerializer.startTag("", SMS_PHONE_NUM_XML);
		xmlSerializer.text(sms.getPhoneNumber());
		xmlSerializer.endTag("", SMS_PHONE_NUM_XML);
		
		if(sms.getPerson()!=null)
		{
			xmlSerializer.startTag("", CONTACT_ID_XML);
			xmlSerializer.text(sms.getPerson().getDisplayName());
			xmlSerializer.endTag("", CONTACT_ID_XML);
			
			xmlSerializer.startTag("", CONTACT_DISPLAY_NAME_XML);
			xmlSerializer.text(sms.getPerson().getDisplayName());
			xmlSerializer.endTag("", CONTACT_DISPLAY_NAME_XML);
		}

		xmlSerializer.startTag("", SMS_TIME_XML);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		xmlSerializer.text(sdf.format(sms.getReceiveTime()));
		xmlSerializer.endTag("", SMS_TIME_XML);

		xmlSerializer.startTag("", SMS_BODY_XML);
		xmlSerializer.text(sms.getSmsBody());
		xmlSerializer.endTag("", SMS_BODY_XML);
		
		xmlSerializer.startTag("", SMS_SAVE_TIME);
		xmlSerializer.text(sdf.format(new Date()));
		xmlSerializer.endTag("", SMS_SAVE_TIME);
	}

	public void Write(String path,String name, Context context) {
		
		//no sdcard
		if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
			return;
		FileOutputStream fOut = null;
		OutputStreamWriter outWriter = null;
		File dirFile = new File(path);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		
		File file = new File(path + name);
		try {
			fOut = new FileOutputStream(file, true);
			outWriter = new OutputStreamWriter(fOut);
			outWriter.write(xmlWriter.toString());
			outWriter.flush();
			outWriter.close();
			fOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

}
